package engine.step;

import util.List;
import engine.GameEngine;
import engine.data.City;
import engine.data.Player;
import engine.data.Unit;
import engine.data.UnitType;

public final class BeginTurnProcessStep extends Step {

	private static Step step;

	public static synchronized Step getStep() {
		if (step == null)
			step = new BeginTurnProcessStep();
		return step;
	}

	public Step execute() {
		// TEST !!
		// try {
		// ByteArrayOutputStream baos = PersistenceTools.saveGameEngine();
		// byte[] bArr = baos.toByteArray();
		// RecordStore recordStore = RecordStore.openRecordStore("CivME", true);
		// recordStore.addRecord(bArr, 0, bArr.length);
		// recordStore.closeRecordStore();
		// recordStore = RecordStore.openRecordStore("CivME", true);
		// Debug.logConsole(recordStore.getNumRecords());
		// bArr = recordStore.getRecord(0); recordStore.closeRecordStore();
		// PersistenceTools.loadGameEngine(new ByteArrayInputStream(bArr));
		// } catch (IOException e) {
		// e.printStackTrace();
		// } catch (RecordStoreException rse) {
		// rse.printStackTrace();
		// }

		Player currPlayer = GameEngine.currPlayer;
		List objects = currPlayer.units;
		for (short i = 0; i < objects.size(); i++)
			GameEngine.map.refreshRangeImages(currPlayer, ((Unit) objects.get(i)).x, ((Unit) objects.get(i)).y,
					UnitType.getRP(((Unit) objects.get(i)).type), false);
		objects = currPlayer.cities;
		for (short i = 0; i < objects.size(); i++)
			GameEngine.map.refreshRangeImages(currPlayer, ((City) objects.get(i)).x, ((City) objects.get(i)).y,
					(byte) 1, false);
		currPlayer.controller.onBeginTurn();
		return NextUnitStep.getStep();
	}
}
